package no.ntnu.fp.messages.impl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.model.Meeting;
import no.ntnu.fp.utils.XmlParser;

public class MeetingRejectedMessage implements Message {
	private final Meeting meeting;
	private final Employee employee;
	
	public MeetingRejectedMessage(Meeting meeting, Employee employee){
		this.meeting = meeting;
		this.employee = employee;
	}
	
	public Meeting getMeeting() {
		return meeting;
	}

	public Employee getEmployee() {
		return employee;
	}

	public MeetingRejectedMessage(){
		this(null, null);
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.MEETING_REJECTED_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document document = XmlParser.parseXmlString(xml);
		Meeting m = new Meeting((Element) document.getElementsByTagName("meeting").item(0));
		Element rejecter = (Element) document.getElementsByTagName("rejecter").item(0);
		Employee e = new Employee((Element) rejecter.getElementsByTagName("employee").item(0));
		return new MeetingRejectedMessage(m, e);
	}
	
	@Override
	public String toString(){
		Document document = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element message = document.createElement("message");
		message.appendChild(this.meeting.toXml(document));
		Element rejecter = document.createElement("rejecter");
		rejecter.appendChild(this.employee.toXml(document));
		message.appendChild(rejecter);
		return getMessageCode() + XmlParser.convertXmlToString(message);
	}

}
